﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
using deckAnalyser.Domain;

namespace deckAnalyser.Controls
{
  public partial class ManaCurveControl : UserControl
  {
    private Deck deck = null;
    public Deck Deck
    {
      get { return deck; }
      set
      {
        if (deck != null)
          deck.OnUpdate -= OnDeckUpdate;

        deck = value;

        if (deck != null)
          deck.OnUpdate += OnDeckUpdate;

        UpdateGraph();
      }
    }

    void OnDeckUpdate(DomainObject d)
    {
      UpdateGraph();
    }



    public ManaCurveControl()
    {
      InitializeComponent();
    }


    public void UpdateGraph()
    {
      GraphPane zgcPane = zgc.GraphPane;
      zgcPane.CurveList.Clear();
      
      if (deck == null)
      {
        zgc.AxisChange();
        zgc.Refresh();
        return;
      }

      int[] histogram = deck.GetManaHistogram(true);


      zgcPane.Title.Text = "Mana Curve";
      zgcPane.XAxis.Title.Text = "Mana";
      zgcPane.YAxis.Title.Text = "Number of Cards";

      PointPairList data = new PointPairList();

      for (int i=0; i < histogram.Length; i++)
      {
        data.Add(i, histogram[i]);
      }

      LineItem curve = zgcPane.AddCurve(null,data, Color.Red, SymbolType.Circle);

      curve.Line.Width = 1.0F;
      curve.Line.Fill = new Fill(Color.White, Color.Red, 45F);
      curve.Symbol.Size = 8.0F;
      curve.Symbol.Fill = new Fill(Color.White);

      zgcPane.Chart.Fill = new Fill(Color.White,Color.FromArgb(255, 255, 200), -45F);
      zgcPane.XAxis.MajorGrid.IsVisible = true;
      zgcPane.XAxis.MajorGrid.Color = Color.LightGray;
      zgcPane.YAxis.MajorGrid.IsVisible = true;
      zgcPane.YAxis.MajorGrid.Color = Color.LightGray;

      zgc.AxisChange();
      zgc.Refresh();
    }
  }
}
